package cn.wen.eeayjoke.adapter.home;

import android.text.TextUtils;

import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import cn.wen.base.adapter.recyclerview.QuickRecyclerViewHolder;
import cn.wen.base.adapter.recyclerview.QuickVAdapter;
import cn.wen.base.image.ImageLoader;
import cn.wen.base.vlayout.DelegateAdapter;
import cn.wen.base.vlayout.layout.GridLayoutHelper;
import cn.wen.base.vlayout.layout.LinearLayoutHelper;
import cn.wen.base.vlayout.layout.OnePlusNLayoutHelper;
import cn.wen.eeayjoke.R;
import cn.wen.eeayjoke.entity.bean.home.AdvBean;
import cn.wen.eeayjoke.entity.bean.home.AdvertisementsBean;
import cn.wen.eeayjoke.entity.bean.home.HomeData;
import cn.wen.eeayjoke.entity.bean.home.IndexTopicItems;
import cn.wen.eeayjoke.entity.bean.home.TargetDetailBean;
import cn.wen.eeayjoke.utils.ItemTypes;

/**
 * 首页Adapter的管理类
 */

public class HomeAdapterManager {

    /**
     * 将Data转成Adapter集合
     */
    public List<DelegateAdapter.Adapter> getAdapters(HomeData data) {
        if (data == null || data.topics == null || data.topics.items == null
                || data.topics.items.size() <= 0) {
            return null;
        }
        List<DelegateAdapter.Adapter> list = new ArrayList<>();
        // 1.顶部Banner
        BannerAdapter bannerAdapter = new BannerAdapter(data.banner, R.layout.main_banner_top) {
            @Override
            public int getItemViewType(int position) {
                return ItemTypes.TYPE_BANNER_TOP;
            }
        };
        list.add(bannerAdapter);
        // 其它的条目
        List<IndexTopicItems> items = data.topics.items;
        for (IndexTopicItems item : items) {
            String key = item.layout_key;
            switch (key) {
                // 2. 小Banner、管理集合
                case "index-category-navigation":
                case "1-1-1-1-1":
                    setNavigationAdapter(list, item);
                    break;
                // 3. 行业头条
                case "index-industry-headlines":
                case "h_y_t_t":
                    setHeadlines(list, item);
                    break;
                // 4. 限时抢购
                case "1-1-2":
                    setLimit(list, item);
                    break;
                // 5. 品牌列表
                case "1-1-1":
                    setBrand(list, item);
                    break;
                // 6. 天天特价、区域特供、底部小Banner
                case "1-2":
                    setDailySpecial(list, item);
                    break;
                //  项目套餐、工厂尾货、热卖推荐、五折专区、开店必备顶部的小图片
                case "2-2":
                    setMeal(list, item);
                    break;
                //  活动专栏，高度为100的bannar
                case "1-1":
                    setActivityBanner(list, item);
                    break;
                //  TODO 聚划算
                case "1_1_1_1":
                    break;
            }
        }
        // 加一个猜你喜欢
        IndexTopicItems bean = new IndexTopicItems();
        bean.pic_path = "http://img.car-house.cn/Upload/businessHome/icon_home_28_alsolike@3x.png";
        SubTitleAdapter adapter = new SubTitleAdapter(bean);
        list.add(adapter);
        return list;
    }

    private void setActivityBanner(List<DelegateAdapter.Adapter> list, IndexTopicItems item) {
        AdvertisementsBean advertisements = item.advertisements;
        if (advertisements != null) {
            List<AdvBean> advBeans = advertisements.items;
            if (advBeans != null && advBeans.size() > 0) {
                BannerAdapter bannerAdapter = new BannerAdapter(advertisements, R.layout.main_banner_activity);
                list.add(bannerAdapter);
            }
        }
    }

    /**
     * 项目套餐、工厂尾货、热卖推荐、五折专区、开店必备顶部的小图片
     */
    private void setMeal(List<DelegateAdapter.Adapter> list, IndexTopicItems item) {
        // 标题
        SubTitleAdapter adapter = new SubTitleAdapter(item);
        list.add(adapter);
        // TODO 四组产品

        List<IndexTopicItems> indexTopicItems = item.indexTopicItems;
        final GridLayoutHelper helper = new GridLayoutHelper(2, indexTopicItems.size());
        QuickVAdapter<IndexTopicItems> gridAdapter =
                new QuickVAdapter<IndexTopicItems>(indexTopicItems, helper, R.layout.main_meal) {
                    @Override
                    protected void convert(QuickRecyclerViewHolder holder, IndexTopicItems item, int position) {
                        TargetDetailBean detail = item.targetDetail;
                        if (detail == null) {
                            return;
                        }
                        if (!TextUtils.isEmpty(item.pic_path)) {
                            holder.displayImage(R.id.iv_icon, item.pic_path, R.drawable.transparent);
                        } else {
                            holder.displayImage(R.id.iv_icon, detail.goodsThumb, R.drawable.transparent);
                        }
                        holder.displayImage(R.id.iv_hot, detail.activityIcon, R.drawable.transparent);
                        holder.setText(R.id.tv_sell_count, "销量:" + detail.favoriteCount);
                        String price = "¥" + detail.supplyPrice;
                        holder.setText(R.id.tv_price, price);
                        holder.setText(R.id.tv_desc, detail.goodsName);
                    }

                    @Override
                    public int getItemViewType(int position) {
                        return ItemTypes.TYPE_ITEM;
                    }
                };
        list.add(gridAdapter);
        // Banner
        AdvertisementsBean advertisements = item.advertisements;
        if (advertisements != null) {
            List<AdvBean> advBeans = advertisements.items;
            if (advBeans != null && advBeans.size() > 0) {
                BannerAdapter bannerAdapter = new BannerAdapter(advertisements, R.layout.main_banner_activity);
                list.add(bannerAdapter);
            }
        }
    }

    /**
     * 6. 天天特价、区域特供、底部小Banner
     */
    private void setDailySpecial(List<DelegateAdapter.Adapter> list, IndexTopicItems item) {
        // 标题
        SubTitleAdapter adapter = new SubTitleAdapter(item);
        list.add(adapter);
        // 数据封装
        List<IndexTopicItems> items = item.indexTopicItems;
        List<IndexTopicItems> ones = new ArrayList<>();
        List<IndexTopicItems> tows = new ArrayList<>();
        for (int i = 0; i < items.size(); i++) {
            if (i <= 2) {
                ones.add(items.get(i));
            } else if (i <= 5) {
                tows.add(items.get(i));
            }
        }
        // 天天特价
        // 1 拖 2
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setColWeights(new float[]{35f, 65f, 65f}); // 2:3的比例
        SpecialAdapter adapter1 = new SpecialAdapter(ones, helper);
        list.add(adapter1);

        // 区域特供
        helper = new OnePlusNLayoutHelper();
        helper.setColWeights(new float[]{35f, 65f, 65f});// 2:3的比例
        helper.setMargin(0, 10, 0, 0);
        SpecialAdapter adapter2 = new SpecialAdapter(tows, helper);
        list.add(adapter2);
        // 底部小Banner
        AdvertisementsBean advertisements = item.advertisements;
        if (advertisements != null) {
            List<AdvBean> advBeans = advertisements.items;
            if (advBeans != null && advBeans.size() > 0) {
                BannerAdapter bannerAdapter = new BannerAdapter(advertisements, R.layout.main_banner_activity);
                list.add(bannerAdapter);
            }
        }
    }

    /**
     * 5. 品牌列表
     */
    private void setBrand(List<DelegateAdapter.Adapter> list, IndexTopicItems item) {
        final LinearLayoutHelper helper = new LinearLayoutHelper();
        List<IndexTopicItems> data = new ArrayList<>();
        data.add(item);
        QuickVAdapter<IndexTopicItems> adapter =
                new QuickVAdapter<IndexTopicItems>(data, helper, R.layout.main_brand) {
                    @Override
                    protected void convert(QuickRecyclerViewHolder holder, IndexTopicItems item, int position) {
                        List<IndexTopicItems> items = item.indexTopicItems;
                        // 背景
                        ImageLoader.displayView(holder.getView(), item.bg_url, R.drawable.transparent);
                        try {
                            // 左边的图标
                            IndexTopicItems left = items.get(0);
                            holder.displayImage(R.id.iv_main_brand_icon, left.pic_path, R.drawable.transparent);
                        } catch (Exception e) {
                        }


                        try {
                            // TODO 中间部分
                        } catch (Exception e) {
                        }

                        try {
                            // 右边的图标
                            IndexTopicItems right = items.get(2);
                            holder.displayImage(R.id.iv_main_brand_more, right.pic_path, R.drawable.transparent);
                        } catch (Exception e) {
                        }

                    }

                    @Override
                    public int getItemViewType(int position) {
                        return ItemTypes.TYPE_BRAND;
                    }
                };
        list.add(adapter);
    }

    /**
     * 4. 限时抢购
     */
    private void setLimit(List<DelegateAdapter.Adapter> list, IndexTopicItems item) {
        // 1 拖 2
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        // 2:3的比例
        helper.setColWeights(new float[]{40f, 60f, 60f});
        DensityUtil densityUtil = new DensityUtil();
        int margin = densityUtil.dip2px(4);
        helper.setPadding(margin, 0, margin, 0);
        List<IndexTopicItems> indexTopicItems = item.indexTopicItems;
        LimitAdapter adapter = new LimitAdapter(indexTopicItems, helper, R.layout.main_limit);
        list.add(adapter);
    }

    /**
     * 3. 行业头条
     */
    private void setHeadlines(List<DelegateAdapter.Adapter> list, IndexTopicItems item) {
        HeadlinesAdapter adapter = new HeadlinesAdapter(item);
        list.add(adapter);
    }

    /**
     * 2. 小Banner、管理集合
     */
    private void setNavigationAdapter(List<DelegateAdapter.Adapter> list, IndexTopicItems item) {
        // 2. 小Banner
        AdvertisementsBean adv = item.advertisements;
        if (adv != null) {
            BannerAdapter bannerAdapter = new BannerAdapter(adv, R.layout.main_banner_activity);
            list.add(bannerAdapter);
        }
        // 3. 管理集合
        final List<IndexTopicItems> topicItems = item.indexTopicItems;
        if (topicItems != null && topicItems.size() > 0) {
            NavigationAdapter navigationAdapter = new NavigationAdapter(topicItems);
            list.add(navigationAdapter);
        }

    }

}
